package com.hy.mechanicalarm.baseapp;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;

import com.hy.mechanicalarm.ui.activity.MainActivity;
import com.hy.mechanicalarm.ui.service.SocketService;
import com.hy.mechanicalarm.utils.CommonUtil;
import com.hy.mechanicalarm.utils.ServiceUtils;

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * Created by 韩银 on 2018/7/31 16:17
 * hanyinit@163.com
 */
public class  BaseActivity extends AppCompatActivity {

    private static long mPreTime;
    private static Activity mCurrentActivity;// 对所有activity进行管理
    public static List<Activity> mActivities = new LinkedList<Activity>();
    protected  Bundle savedInstanceState;
    private static long lastClickTime = System.currentTimeMillis();
    public SocketService socketService;
    public ServiceConnection sc;
    public Intent mServiceIntent;
    @Override
    public final void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.savedInstanceState = savedInstanceState;

        //初始化的时候将其添加到集合中
        synchronized (mActivities) {
            mActivities.add(this);
        }

        bindSocketService();
        initView();
        initData();
        initListener();
    }
    public void bindSocketService() {

        /*通过binder拿到service*/
        sc = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                SocketService.SocketBinder binder = (SocketService.SocketBinder) iBinder;
                socketService = binder.getService();

            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {

            }
        };
        mServiceIntent = new Intent(this, SocketService.class);
        bindService(mServiceIntent, sc, BIND_AUTO_CREATE);
    }


    public void initView() {
    }

    public void initData() {
    }

    public void initListener() {
    }



    @Override
    protected void onResume() {
        super.onResume();
        mCurrentActivity = this;
    }

    /*@Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if(ev.getAction() == MotionEvent.ACTION_DOWN){
            if(isFastDoubleClick()){
                return true;
            }
        }
        return super.dispatchTouchEvent(ev);
    }*/

    public static boolean isFastDoubleClick(){
        long time = System.currentTimeMillis();
        long timeDouble = time - lastClickTime;
        lastClickTime = time;
        return timeDouble < 400;
    }
    @Override
    protected void onPause() {
        super.onPause();
        mCurrentActivity = null;
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        //销毁的时候从集合中移除
        synchronized (mActivities) {
            mActivities.remove(this);
        }
        unbindService(sc);
    }

    /**
     * 退出应用的方法
     */
    public static void exitApp() {

        ListIterator<Activity> iterator = mActivities.listIterator();

        while (iterator.hasNext()) {
            Activity next = iterator.next();
            next.finish();
        }
    }

    public static Activity getCurrentActivity() {
        return mCurrentActivity;
    }

    /**
     * 统一退出控制
     */
    @Override
    public void onBackPressed() {
        if (mCurrentActivity instanceof MainActivity){
            //如果是主页面
            if (System.currentTimeMillis() - mPreTime > 2000) {// 两次点击间隔大于2秒
                CommonUtil.showToast("再按一次，退出应用");
                mPreTime = System.currentTimeMillis();
                return;
            }
        }
        super.onBackPressed();// finish()
    }






}